package cn.superchart.module.dev.genconfig.service.impl;

import cn.superchart.common.base.BaseServiceImpl;
import cn.superchart.common.utils.Checker;
import cn.superchart.module.dev.gencode.entity.ScGenTable;
import cn.superchart.module.dev.gencode.service.ScGenTableService;
import cn.superchart.module.dev.genconfig.entity.ScGenTableConf;
import cn.superchart.module.dev.genconfig.mapper.ScGenTableConfMapper;
import cn.superchart.module.dev.genconfig.param.*;
import cn.superchart.module.dev.genconfig.service.ScGenTableConfService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Slf4j
@Service
public class ScGenTableConfServiceImpl extends BaseServiceImpl<ScGenTableConf, ScGenTableConfMapper> implements ScGenTableConfService {


    @Autowired
    private ScGenTableService scGenTableService;

    @Override
    public String add(ScGenTableConfAddParam scGenTableConfAddParam) {
        return null;
    }

    @Override
    public void edit(ScGenTableConfEditParam scGenTableConfEditParam) {

    }

    @Override
    public ScGenTableConfDetailParam detail(String id) {
        return null;
    }

    @Override
    public Page<ScGenTableConfDetailParam> page(ScGenTableConfPageParam scGenTableConfPageParam) {
        return null;
    }

    @Override
    public List<ScGenTableConfDetailParam> fieldQuery(ScTableConfQueryParam scTableConfQueryParam) {
        ScGenTable scGenTable = scGenTableService.queryEntity(scTableConfQueryParam.getBaseId());
        List<ScGenTableConfDetailParam> fieldSqlQueries =
        baseMapper.fieldQuery(scTableConfQueryParam.getTableSchema(),scGenTable.getTbTableName(), scTableConfQueryParam.getBaseId());
        return Checker.BeNotEmpty(fieldSqlQueries)?fieldSqlQueries: Lists.newArrayList();
    }


    @Transactional
    @Override
    public void upConf(ScGenTableConfBatchAddParam scGenTableConfBatchAddParam) {
         if(Checker.BeNotEmpty(scGenTableConfBatchAddParam.getConfigs())){
                 String baseId = scGenTableConfBatchAddParam.getBaseId();
                 delete(baseId);
                 scGenTableConfBatchAddParam.getConfigs().forEach(conf->conf.setBasicId(baseId));
                 saveData(scGenTableConfBatchAddParam.getConfigs());
         }
    }

    public void delete(String baseId) {
        LambdaQueryWrapper<ScGenTableConf> queryWrapper = queryWrapper().lambda().eq(ScGenTableConf::getBasicId,baseId);
        super.remove(queryWrapper);
    }
}
